import { getTokenInfo } from '@/utils/storage'
const initState = {
  profile: { }as ProfileInfo,
  auth: getTokenInfo(),
  user: {} as User,
}
export type Auth = {
  token: string
  refresh_token: string
}
export type User={
  id: string
  name: string
  photo: string
  art_count: number
  follow_count: number
  fans_count: number
  like_count: number
}
export type ProfileInfo = {
    id: string
    photo: string
    name: string
    mobile: string
    gender: number
    birthday: string
    intro: string | null
}
    // 定义 action 类型
export type UserAction = { type: 'user/saveAuth'; payload: Auth }|{ type: 'user/saveUser'; payload: User }| { type: 'user/saveProfile'; payload: ProfileInfo }| { type: 'user/clearAuth' }
export  function userReducer(state = initState, action: UserAction) {
// export default function userReducer(state = initState, action: UserAction) {
  switch (action.type) {
    case 'user/saveAuth':
      return { ...state, auth: action.payload }
    case 'user/saveUser':
      return{...state,user:action.payload}
    case 'user/saveProfile':
      return{
        ...state,profile:action.payload
      }
      case 'user/clearAuth':
        return { ...state, auth: {} as Auth }
    default:
      return state
  }
}